// 引入微信小程序版本的腾讯地图 SDK
const QQMapWX = require('../libs/qqmap-wx-jssdk.min.js');

class QQMapUtil {
  constructor() {
    this.qqmapsdk = new QQMapWX({
      key: 'CFUBZ-GRYCJ-IAJFO-DT56D-EUSBE-RTBGY'
    });
  }

  // 地址解析
  geocoder(address) {
    return new Promise((resolve, reject) => {
      this.qqmapsdk.geocoder({
        address: address,
        success: res => {
          resolve(res);
        },
        fail: err => {
          reject(err);
        }
      });
    });
  }

  // 逆地址解析
  reverseGeocoder(location) {
    return new Promise((resolve, reject) => {
      this.qqmapsdk.reverseGeocoder({
        location: {
          latitude: location.latitude,
          longitude: location.longitude
        },
        success: res => {
          resolve(res);
        },
        fail: err => {
          reject(err);
        }
      });
    });
  }

  // 搜索周边
  searchNearby(keyword, location, radius = 1000) {
    return new Promise((resolve, reject) => {
      this.qqmapsdk.search({
        keyword: keyword,
        location: `${location.latitude},${location.longitude}`,
        radius: radius,
        success: res => {
          resolve({
            detail: {
              pois: res.data.map(item => ({
                id: item.id,
                name: item.title,
                address: item.address,
                distance: item._distance,
                latLng: {
                  lat: item.location.lat,
                  lng: item.location.lng
                }
              }))
            }
          });
        },
        fail: err => {
          reject(err);
        }
      });
    });
  }
}

export default new QQMapUtil(); 